package sz.szsmk.citizencard.ui.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.log.L;
import sz.szsmk.citizencard.model.network.bean.response.GetResourceReq;
import sz.szsmk.citizencard.model.network.bean.response.QueryInterductionAreaRes;
import sz.szsmk.citizencard.retrofitrxjava.ErrorAction;
import sz.szsmk.citizencard.retrofitrxjava.NetworkDateSources;
import sz.szsmk.citizencard.retrofitrxjava.SubscribeResult;
import sz.szsmk.citizencard.util.Constant;
import sz.szsmk.citizencard.util.JumpThirdView;
import sz.szsmk.citizencard.widget.MarqueeView;

/**
 * Copyright (c)2019
 * author: zdh
 * created on: 2019/1/6 16:47
 * description:
 */
@SuppressLint("ValidFragment")
public class AnnouncementFragment extends Fragment {

    public static final int REQ_SUCCESS = 1;

    private ArrayList<String> stringList = new ArrayList<String>();
    private View view;
    private List<QueryInterductionAreaRes.InterdutionListBean> interdution_list = new ArrayList<>();
    private QueryInterductionAreaRes.InterdutionListBean interdutionListBean;
    private MarqueeView mMarqueeView;
    private List<QueryInterductionAreaRes.InterdutionListBean> viewData = new ArrayList<>();
    private int mNendHeader;

    @SuppressLint("ValidFragment")
    public AnnouncementFragment(List<QueryInterductionAreaRes.InterdutionListBean> viewData) {
        this.viewData = viewData;
    }

    @Override
    public View onCreateView(LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.announcement_fragment, null);
        if (viewData != null) {
            mMarqueeView = (MarqueeView) view.findViewById(R.id.apphome_banner_vt);
            mMarqueeView.startWithList(stringList);
            for (int i = 0; i < viewData.size(); i++) {
                String content = viewData.get(i).getContent();
                stringList.add(content);
            }
            mMarqueeView.startWithList(stringList);
            mMarqueeView.setOnItemClickListener(new MarqueeView.OnItemClickListener() {

                private int mNendHeader;
                private QueryInterductionAreaRes.InterdutionListBean interdutionListBean;

                @Override
                public void onItemClick(int position, TextView textView) {
                    if (viewData != null) {
                        interdutionListBean = viewData.get(position);
                        String link_url = interdutionListBean.getLink_url();
                        String need_header = interdutionListBean.getNeed_header();
                        if (need_header != null) {
                            mNendHeader = Integer.valueOf(need_header).intValue();
                        }
                        String sys_label = interdutionListBean.getSys_label();
                        JumpThirdView.baseToJumpView(getActivity(), link_url, sys_label, mNendHeader, "");
                    }
                }
            });
        }

//
        return view;
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

//        getData();

    }

    private void getData() {
        GetResourceReq getResourceRes = new GetResourceReq();
        getResourceRes.setResource_area_id(42);
        NetworkDateSources sources = NetworkDateSources.getInstance();
        String json = new Gson().toJson(getResourceRes);
        L.e(Constant.TAG, "--------------" + json);
        sources.queryInterductionArea(getResourceRes, new SubscribeResult<QueryInterductionAreaRes>() {
            @Override
            protected void onOk(QueryInterductionAreaRes response) {
                interdution_list = response.getInterdution_list();
                L.e(Constant.TAG, "--------------" + new Gson().toJson(response));
                for (int i = 0; i < interdution_list.size(); i++) {
//                    titleList.add(interdution_list.get(i).getContent());
                }


            }

            @Override
            protected void onFailure(QueryInterductionAreaRes response) {
                super.onFailure(response);
                Toast.makeText(getActivity(), response.getMessage(), Toast.LENGTH_SHORT).show();
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

            }
        });

    }

}
